<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\UsersMember as UsersMemberModel;

class UsersMemberService
{
    /**
     * 会员统计
     * @param array $where
     * @return int
     */
    public static function countUsersMember($where = [])
    {
        return UsersMemberModel::countUsersMember($where);
    }

    /**
     * 会员列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getUsersMemberList($where, $page, $pagesize)
    {
        $list = UsersMemberModel::getUsersMemberList($where, $page, $pagesize);
        foreach ($list as $k => $v) {
            $list[$k]['ids'] = $v['id'] . '<br><span class="color"> ' . $v['uid'].'</span>';
            $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div>';

            $list[$k]['nickname_data'] = '<div class="d-fix1 mar-lefts">' . $headimg . '<div><p class="datas">' . emojiDecode($v['nickname']) . '</p>';

            $list[$k]['create_time'] = date('Y-m-d', strtotime($v['create_time']));
            $new_time = $v['end_time'] - time();
            // 会员天数
            $member_day = round($new_time / 86400);
            // 会员套餐
            $unit = MemberLevelService::findMemberLevelValue(['id' => $v['mid']]) ?: '天';
            $list[$k]['vip_meal'] = $member_day . '<br><span class="color">' . $unit . '</span>';
            switch ($v['type']) {
                case 1:
                    $type = '<span class="color1">后台升级</span>';
                    break;
                case 2:
                    $type = '<span class="color2">会员赠送</span>';
                    break;
                default:
                    $type = '<span class="color3">自行购买</span>';
            }
            // 会员类型
            $list[$k]['buy_type'] = $type;
        }

        return $list;
    }

    /**
     * 会员列表统计
     * @param $where
     * @return mixed
     */
    public static function countUsersMemberList($where)
    {
        return UsersMemberModel::countUsersMemberList($where);
    }

    /**
     * 失效会员统计
     * @param array $where
     * @return int
     */
    public static function countInvalidUsersMember($where = [])
    {
        return UsersMemberModel::countInvalidUsersMember($where);
    }

    /**
     * 会员用户数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findUsersMember($where)
    {
        return UsersMemberModel::findUsersMember($where);
    }

    /**
     * 更新会员用户信息
     * @param $data
     * @param $where
     * @return UsersMemberModel
     */
    public static function updateUsersMember($data, $where)
    {
        return UsersMemberModel::updateUsersMember($data, $where);
    }

    /**
     * 创建会员用户信息
     * @param $data
     * @return UsersMemberModel|\think\Model
     */
    public static function createUsersMember($data)
    {
        return UsersMemberModel::createUsersMember($data);
    }

    /**
     * 获取本月会员用户（折线图）
     * @param array $where
     * @return mixed
     */
    public static function getUsersMemberMonthLine($where = [])
    {
        $startEndTime = getStartEndTime();
        $usersMemberMonth = UsersMemberModel::getUsersMemberMonth1($where, $startEndTime['startTime'], $startEndTime['endTime']);
        return inArr($usersMemberMonth, $startEndTime['dateArr']);
    }

    /**
     * 本周会员总数
     * @param $where
     * @return int
     */
    public static function countUsersMemberWeek($where)
    {
        return UsersMemberModel::countUsersMemberWeek($where);
    }

    /**
     * 本月会员总数
     * @param $where
     * @return int
     */
    public static function countUsersMemberMonth($where)
    {
        return UsersMemberModel::countUsersMemberMonth($where);
    }

}